package com.blkan.calculator.util;

import android.widget.TextView;

import com.blkan.calculator.Calculator;
import com.blkan.calculator.databinding.ActivityMainBinding;
import com.blkan.calculator.databinding.FragmentCalculatorBinding;
import com.blkan.calculator.fragments.CalculatorFragment;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

@Deprecated
public class Input {
    @Deprecated
    public static StringBuilder input = new StringBuilder("");
    @Deprecated
    public static String result = "0";
    @Deprecated
    public static boolean deg = false;

    @Deprecated
    public static void initButtons(FragmentCalculatorBinding binding, boolean portrait) {
        updateTextView(binding.tvInput);
        binding.tvAnswer.setText(result);
        binding.btnAc.setOnClickListener(view -> {
            input = new StringBuilder("");
            updateTextView(binding.tvInput);
        });
        binding.btnBack.setOnClickListener(view -> {
            if (input.length() == 0)
                return;
            int cut = backSpace();
            input.delete(input.length() - cut, input.length());
            updateTextView(binding.tvInput);
        });
        binding.btnPi.setOnClickListener(view -> {
            input.append("π");
            updateTextView(binding.tvInput);
        });
        binding.btnAdd.setOnClickListener(view -> {
            addSign("+");
            updateTextView(binding.tvInput);
        });
        binding.btn7.setOnClickListener(view -> {
            addNumber(7);
            updateTextView(binding.tvInput);
        });
        binding.btn8.setOnClickListener(view -> {
            addNumber(8);
            updateTextView(binding.tvInput);
        });
        binding.btn9.setOnClickListener(view -> {
            addNumber(9);
            updateTextView(binding.tvInput);
        });
        binding.btnMinus.setOnClickListener(view ->
        {
            addSign("-");
            updateTextView(binding.tvInput);
        });
        binding.btn4.setOnClickListener(view -> {
            addNumber(4);
            updateTextView(binding.tvInput);
        });
        binding.btn5.setOnClickListener(view -> {
            addNumber(5);
            updateTextView(binding.tvInput);
        });
        binding.btn6.setOnClickListener(view -> {
            addNumber(6);
            updateTextView(binding.tvInput);
        });
        binding.btnMultiply.setOnClickListener(view ->
        {
            addSign("×");
            updateTextView(binding.tvInput);
        });
        binding.btn1.setOnClickListener(view -> {
            addNumber(1);
            updateTextView(binding.tvInput);
        });
        binding.btn2.setOnClickListener(view -> {
            addNumber(2);
            updateTextView(binding.tvInput);
        });
        binding.btn3.setOnClickListener(view -> {
            addNumber(3);
            updateTextView(binding.tvInput);
        });
        binding.btnDivide.setOnClickListener(view ->
        {
            addSign("÷");
            updateTextView(binding.tvInput);
        });
        binding.btnAns.setOnClickListener(view -> {
            input.append("Ans");
            updateTextView(binding.tvInput);
        });
        binding.btn0.setOnClickListener(view -> {
            addNumber(0);
            updateTextView(binding.tvInput);
        });
        binding.btnPoint.setOnClickListener(view -> {
            if (input.length() == 0)
                input.append(0);
            char c = input.charAt(input.length() - 1);
            if (c >= '0' && c <= '9') {
                input.append(".");
                updateTextView(binding.tvInput);
            }
        });
        binding.btnEqual.setOnClickListener(view -> {
            String s = input.toString();
            Pattern pattern = Pattern.compile("×");
            Matcher matcher = pattern.matcher(s);
            s = matcher.replaceAll("*");

            pattern = Pattern.compile("÷");
            matcher = pattern.matcher(s);
            s = matcher.replaceAll("/");

            pattern = Pattern.compile("(?<=\\A|\\+|-|\\*|\\/|\\^)(Ans)");
            matcher = pattern.matcher(s);
            s = matcher.replaceAll('(' + result + ')');

            pattern = Pattern.compile("Ans");
            matcher = pattern.matcher(s);
            s = matcher.replaceAll("*" + '(' + result + ')');

            result = Calculator.getInstance().calExpression(s, deg, false);
            if (result.endsWith(".0"))
                result = result.substring(0, result.length() - 2);
            binding.tvAnswer.setText(result);
        });
        if (portrait)
            return;
        if (deg)
            binding.btnRad.setText("Deg");
        binding.btnRad.setOnClickListener(view -> {
            deg = !deg;
            if (deg)
                binding.btnRad.setText("Deg");
            else
                binding.btnRad.setText("Rad");
        });
        binding.btnLeftP.setOnClickListener(view -> {
            if (input.length() != 0) {
                char c = input.charAt(input.length() - 1);
                if ((c >= '0' && c <= '9') || c == ')' || c == 'e' || c == 'π' || c == 's' || c == '!') {
                    input.append("×");
                }
            }
            input.append('(');
            updateTextView(binding.tvInput);
        });
        binding.btnRightP.setOnClickListener(view -> {
            if (input.length() != 0) {
                input.append(')');
                updateTextView(binding.tvInput);
            }
        });
        binding.btnSin.setOnClickListener(view -> {
            input.append("sin(");
            updateTextView(binding.tvInput);
        });
        binding.btnCos.setOnClickListener(view -> {
            input.append("cos(");
            updateTextView(binding.tvInput);
        });
        binding.btnTan.setOnClickListener(view -> {
            input.append("tan(");
            updateTextView(binding.tvInput);
        });
        binding.btnArcsin.setOnClickListener(view -> {
            input.append("arcsin(");
            updateTextView(binding.tvInput);
        });
        binding.btnArccos.setOnClickListener(view -> {
            input.append("arccos(");
            updateTextView(binding.tvInput);
        });
        binding.btnArctan.setOnClickListener(view -> {
            input.append("arctan(");
            updateTextView(binding.tvInput);
        });
        binding.btnLg.setOnClickListener(view -> {
            input.append("lg(");
            updateTextView(binding.tvInput);
        });
        binding.btnLg.setOnClickListener(view -> {
            input.append("lg(");
            updateTextView(binding.tvInput);
        });
        binding.btnLn.setOnClickListener(view -> {
            input.append("ln(");
            updateTextView(binding.tvInput);
        });
        binding.btnE.setOnClickListener(view -> {
            input.append('e');
            updateTextView(binding.tvInput);
        });
        binding.btnPow.setOnClickListener(view -> {
            addSign("^(");
            updateTextView(binding.tvInput);
        });
        binding.btnSqrt.setOnClickListener(view -> {
            input.append("sqrt(");
            updateTextView(binding.tvInput);
        });
        binding.btnFact.setOnClickListener(view -> {
            if (input.length() == 0)
                return;
            char c = input.charAt(input.length() - 1);
            if (c == '(' || c == '!')
                return;
            if (c == '+' || c == '-' || c == '×' || c == '÷') {
                input.deleteCharAt(input.length() - 1);
            }
            input.append('!');
            updateTextView(binding.tvInput);
        });
    }

    @Deprecated
    private static void addNumber(int number) {
        if (input.length() != 0) {
            char c = input.charAt(input.length() - 1);
            if (c == ')' || c == 'e' || c == 'π' || c == 's' || c == '!') {
                input.append("×");
            }
        }
        input.append(number);
    }

    @Deprecated
    private static void addSign(String sign) {
        if (input.length() == 0)
            input.append(0);
        else {
            char c = input.charAt(input.length() - 1);
            if (c == '+' || c == '-' || c == '×' || c == '÷') {
                input.deleteCharAt(input.length() - 1);
            }
        }
        input.append(sign);
    }

    @Deprecated
    private static int backSpace() {
        String s = input.toString();
        if (s.endsWith("^("))
            return 2;
        if (s.endsWith("lg(") || s.endsWith("ln(") || s.endsWith("Ans"))
            return 3;
        if (s.endsWith("sin(") || s.endsWith("cos(") || s.endsWith("tan("))
            return 4;
        if (s.endsWith("sqrt("))
            return 5;
        if (s.endsWith("arcsin(") || s.endsWith("arccos(") || s.endsWith("arctan("))
            return 7;
        return 1;
    }

    @Deprecated
    private static void updateTextView(TextView tv) {
        tv.setText(input.toString());
    }
}
